package omm.domain.evolutiongraph;

import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import omm.domain.CommissionStrategy;
import omm.domain.Quotation;

import org.joda.time.DateTime;

public class ThousandsUnitStrategy implements UnitStrategy {

	@Override
	public Map<DateTime, Double> calculateByPeriod(List<Quotation> quotations, PeriodStrategy period,
			CommissionStrategy commission) {

		Map<DateTime, Double> barThousands = period.calculatePeriods();

		Iterator<Quotation> it = quotations.iterator();
		while (it.hasNext()) {
			Quotation q = it.next();

			if (commission.conditionToKeep(q.getState())) {

				DateTime modificationDate = q.getModificationDate();
				if (modificationDate != null){
					for (Map.Entry<DateTime, Double> e : barThousands.entrySet()) {
						DateTime dateTime = e.getKey();
						// Comparo si pertenece al periodo
						if (period.isInPeriod(modificationDate, dateTime)) {
							Double actualValue = e.getValue();
							barThousands.put(dateTime, actualValue + q.getAonCommision());
							break;
						}
					}
				}
			}
		}
		
		for (Entry<DateTime, Double> e : barThousands.entrySet()) {
			System.out.println("=========MES:"+e.getKey()+" MILES:"+e.getValue());
		}
		System.out.println();
		return barThousands;
	}

	@Override
	public Integer calculateMaxYAxis(Integer maxYAxis) {
		return maxYAxis /= 1000;
	}

	@Override
	public String getTitle(CommissionStrategy commision) {
		return "Cotizaciones+"+commision.getTitle()+"+en+miles($)";
	}

}
